package chapter06

/**
  * 偏函数(了解)
  */
object Test06_PartialFunction {

  def main(args: Array[String]): Unit = {
    val list = List(("a", 12), ("b", 13), ("c", 14), ("d", 15))

    // 实现key不变,value为两倍

    // 1. map
    val list1: List[(String, Int)] = list.map(tup => (tup._1, tup._2 * 2))

    println(list1)

    // 2.模式匹配
    val list2: List[(String, Int)] = list.map(tuple => tuple match {
      case (a, b) => (a, b * 2)
    })
    println(list2)

    // 3.偏函数
    val list3: List[(String, Int)] = list.map {
      case (a, b) => (a, b * 2)
    }
    println(list3)

    // 4.偏函数应用 positiveAbs:PartialFunction[Int,Int]   -- [参数，返回值]
    val positiveAbs: PartialFunction[Int, Int] = {
      case x if x > 0 => x

    }

    val negativeAbs: PartialFunction[Int, Int] = {
      case x if x < 0 => -x

    }

    val zero: PartialFunction[Int, Int] = {
      case 0 => 0

    }

    def abs(x: Int): Int = (positiveAbs orElse negativeAbs orElse zero) (x): Int

    println(abs(11))

  }
}
